<?php
require_once 'Sabai/Controller/Front.php';

class Xigg_Admin_Node extends Sabai_Controller_Front
{
	function Xigg_Admin_Node()
	{
		parent::Sabai_Controller_Front('List', 'Xigg_Admin_Node_', dirname(__FILE__) . '/Node');
		$this->addControllerFilter(array('Comment', 'Trackback', 'Vote', 'Update', 'Delete', 'View'), 'isValidNodeRequested');
	}

	function _getRoutes()
	{
        $routes['node/list'] = array('controller' => 'List');
        $routes['node/submit'] = array('controller' => 'Submit');
        $routes['node/:node_id/comment'] = array(
                                              'controller'   => 'Comment',
                                              'requirements' => array(':node_id' => '\d+'));
        $routes['node/:node_id/trackback'] = array(
                                                'controller'   => 'Trackback',
                                                'requirements' => array(':node_id' => '\d+'));
        $routes['node/:node_id/vote'] = array(
                                           'controller'   => 'Vote',
                                           'requirements' => array(':node_id' => '\d+'));
        $routes['node/:node_id/edit'] = array(
                                           'controller'   => 'Update',
                                           'requirements' => array(':node_id' => '\d+'));
        $routes['node/:node_id/delete'] = array(
                                           'controller'   => 'Delete',
                                           'requirements' => array(':node_id' => '\d+'));
        $routes['node/:node_id'] = array(
                                           'controller'   => 'View',
                                           'requirements' => array(':node_id' => '\d+'));
        return $routes;
	}

    function isValidNodeRequestedBeforeFilter(&$context)
    {
        if (0 < $node_id = $context->request->getAsInt('node_id')) {
            $model =& $this->_locator->getService('Model');
            $node_r =& $model->getRepository('Node');
            if (false !== $node =& $node_r->fetchById($node_id)) {
                $context->response->setVars(array('node_id' => $node_id,
                                         'node'    => &$node));
                return;
            }
        }
        $context->response->setError(_('Invalid node'), Sabai_URL::get('/node'));
        $context->response->send();
        exit;
    }
}